﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.quest.Managers {
    import flash.events.*;
    import Engine.Managers.*;
    import Events.*;
    import flash.utils.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import __AS3__.vec.*;
    import Display.*;
    import Modules.quest.Display.*;
    import Managers.*;
    import com.adobe.utils.*;

    public class GameQuestManager {

        protected static const QUEST_UX_LOCKING_STARTED:String = "qux_started";
        public static const QUEST_UX_CROPS_UNLOCKED:String = "qux_crops";
        public static const QUEST_UX_RENT_UNLOCKED:String = "qux_rent";
        public static const QUEST_UX_BIZ_UNLOCKED:String = "qux_biz";
        public static const QUEST_UX_VISITS_UNLOCKED:String = "qux_visits";
        public static const QUEST_UX_SUPPLY_MQ_UNLOCKED:String = "qux_supply_mq";
        public static const QUEST_UX_HQ_MQ_UNLOCKED:String = "qux_hq_mq";
        public static const QUEST_UX_PREMIUM_GOODS_UNLOCKED:String = "qux_premiumGoodsBar";
        public static const QUEST_UX_CRUISESHIP:String = "qux_cruise";
        public static const QUEST_UX_ZOO_UNLOCK:String = "zooUnlock_enclosure_jungle";
        public static const ALL_QUEST_UX_FLAGS:Array = [QUEST_UX_CROPS_UNLOCKED, QUEST_UX_RENT_UNLOCKED, QUEST_UX_BIZ_UNLOCKED, QUEST_UX_VISITS_UNLOCKED, QUEST_UX_SUPPLY_MQ_UNLOCKED, QUEST_UX_HQ_MQ_UNLOCKED, QUEST_UX_PREMIUM_GOODS_UNLOCKED, QUEST_UX_CRUISESHIP, QUEST_UX_ZOO_UNLOCK];
        private static const PING_DELAY_MS:int = 5000;

        private var m_timer:Timer;
        private var m_loadedQuestData:Object;
        private var m_saveRequired:Boolean = false;
        private var m_currentQuest:GameQuest;
        private var m_expiredQuests:Array;
        public var m_activeDialogDisplayed:Boolean = false;

        public function GameQuestManager(){
            var _local1:QuestManager = QuestManager.getInstance();
            _local1.addEventListener(QuestEvent.PROGRESS, this.onQuestProgress);
            _local1.addEventListener(QuestEvent.COMPLETED, this.onQuestCompleted);
            _local1.addEventListener(QuestEvent.EXPIRED, this.onQuestExpired);
            this.m_expiredQuests = new Array();
        }
        public function loadQuestData(_arg1:Object=null):void{
            this.m_loadedQuestData = _arg1;
        }
        public function startUp():void{
            this.m_timer = new Timer(PING_DELAY_MS, 10);
            this.m_timer.addEventListener(TimerEvent.TIMER, this.onStartupTimer);
            this.m_timer.start();
        }
        public function callEpilogueWithUrl(_arg1:String, _arg2:String):void{
            var _local3:Array = [_arg2];
            this.displayEpiloguePopups(this.m_currentQuest, _local3);
        }
        protected function onStartupTimer(_arg1:TimerEvent):void{
            if (this.m_loadedQuestData != null){
                this.m_timer.stop();
                this.m_timer.removeEventListener(TimerEvent.TIMER, this.onStartupTimer);
                this.m_timer = null;
            };
            GameTransactionManager.addTransaction(new TPingFeedQuests());
            Global.questManager.refreshActiveIconQuests();
        }
        protected function onQuestCompleted(_arg1:QuestEvent):void{
            var quest:* = null;
            var event:* = _arg1;
            quest = (event.quest as GameQuest);
            if (quest.hasCompleted){
                return;
            };
            quest.hasCompleted = true;
            this.m_currentQuest = quest;
            quest.hideIcon = true;
            TimerUtil.callLater(function ():void{
                var _local2:String;
                Global.hud.removeQuestSprite(quest.name);
                quest.awardQuestRewards();
                quest.doQuestCompleteUxUnlocks();
                if (((quest.hasConsumptions()) && (!(quest.wasAnyTaskPurchased())))){
                    quest.doQuestCompleteConsumptions();
                };
                displayQuestPopup((quest.name + "_complete"), quest, true);
                displayEpiloguePopups(quest);
                StatsManager.milestone(getMilestoneName(quest.name, "complete"));
                Global.hud.refreshGoodsHUD();
                var _local1:Vector.<String> = quest.getSequels();
                for each (_local2 in _local1) {
                    StatsManager.milestone(getMilestoneName(_local2, "begin"));
                };
                if (quest.completionCallback != null){
                    quest.completionCallback();
                };
                GameTransactionManager.addTransaction(new TPingFeedQuests());
            }, (quest.completeWaitTime * 1000));
        }
        protected function onQuestExpired(_arg1:QuestEvent):void{
            Global.hud.removeQuestSprite(_arg1.quest.name);
            this.m_expiredQuests.push(_arg1.quest.name);
        }
        protected function onQuestProgress(_arg1:QuestEvent):void{
            var _local2:GameQuest = (_arg1.quest as GameQuest);
            _local2.updateProgressFlag();
            if (_arg1.taskCompleted != -1){
                if (!((((_local2.hasIntro) && ((_local2.tasks[_arg1.taskCompleted].action == "seenQuest")))) || (((_local2.popNews) && ((_local2.tasks[_arg1.taskCompleted].action == "popNews")))))){
                    Global.hud.showGoalsProgressOverlayOnQuestIcon(_local2.name, "progress");
                    StatsManager.milestone(this.getMilestoneName(_local2.name, ("progress_" + _arg1.taskCompleted)));
                };
            };
        }
        protected function getMilestoneName(_arg1:String, _arg2:String):String{
            return (((("quest_" + _arg1) + "_") + _arg2));
        }
        private function hasRecentlyExpired(_arg1:String):Boolean{
            var _local3:String;
            var _local2:Boolean;
            for each (_local3 in this.m_expiredQuests) {
                if (_arg1 == _local3){
                    _local2 = true;
                };
            };
            return (_local2);
        }
        public function hasSeenQuestIntro(_arg1:String):Boolean{
            var _local3:GameQuest;
            var _local4:Object;
            var _local2:Object = Global.questManager.getQuestProgressByName(_arg1);
            if (((_local2) && (!(Global.player.isQuestCompleted(_arg1))))){
                _local3 = Global.questManager.getQuestByName(_arg1);
                if (_local3){
                    _local4 = _local3.getPopupData();
                    if (((_local3.hasIntro) && (_local3.hasDoneTask("seenQuest", _local4.tasks, _local4.progress)))){
                        return (true);
                    };
                };
            } else {
                if (Global.player.isQuestCompleted(_arg1)){
                    _local3 = Global.questManager.getQuestByName(_arg1);
                    if (((_local3) && (_local3.hasIntro))){
                        return (true);
                    };
                };
            };
            return (false);
        }
        private function questHasNotRecentlyExpired(_arg1:Quest):Boolean{
            var _local3:String;
            var _local2:Boolean;
            for each (_local3 in this.m_expiredQuests) {
                if (_arg1.name == _local3){
                    _local2 = false;
                };
            };
            return (_local2);
        }
        public function getActiveQuests():Vector.<Quest>{
            var _local3:Quest;
            var _local1:Vector.<Quest> = QuestManager.getInstance().getActiveQuests();
            var _local2:int;
            while (_local2 < _local1.length) {
                _local3 = _local1[_local2];
                if (this.questHasNotRecentlyExpired(_local3)){
                    _local2++;
                } else {
                    _local1.splice(_local2, 1);
                };
            };
            return (_local1);
        }
        public function getIncompleteActiveQuests():Vector.<Quest>{
            var _local3:Quest;
            var _local1:Vector.<Quest> = QuestManager.getInstance().getIncompleteActiveQuests();
            var _local2:int;
            while (_local2 < _local1.length) {
                _local3 = _local1[_local2];
                if (this.questHasNotRecentlyExpired(_local3)){
                    _local2++;
                } else {
                    _local1.splice(_local2, 1);
                };
            };
            return (_local1);
        }
        public function getQuestProgressByName(_arg1:String):Object{
            if (this.hasRecentlyExpired(_arg1)){
                return (null);
            };
            return (QuestManager.getInstance().getQuestProgressByName(_arg1));
        }
        public function getQuestByName(_arg1:String):GameQuest{
            var _local3:Quest;
            var _local2:Vector.<Quest> = this.getActiveQuests();
            for each (_local3 in _local2) {
                if ((((_local3 is GameQuest)) && ((_local3.name == _arg1)))){
                    return ((_local3 as GameQuest));
                };
            };
            return (null);
        }
        public function setQuestTaskProgress(_arg1:String, _arg2:int, _arg3:int):void{
            if (this.hasRecentlyExpired(_arg1)){
                return;
            };
            QuestManager.getInstance().setQuestTaskProgress(_arg1, _arg2, _arg3);
        }
        public function refreshActiveIconQuests():void{
            var _local2:Quest;
            var _local1:Vector.<Quest> = this.getIncompleteActiveQuests();
            for each (_local2 in _local1) {
                if ((_local2 is GameQuest)){
                    if (this.questHasNotRecentlyExpired(_local2)){
                        (_local2 as GameQuest).addToQuestBar();
                    };
                };
            };
        }
        public function pumpActivePopup(_arg1:String):void{
            var _local3:Quest;
            var _local4:GameQuest;
            var _local2:Vector.<Quest> = this.getActiveQuests();
            for each (_local3 in _local2) {
                if ((((_local3 is GameQuest)) && ((_local3.name == _arg1)))){
                    _local4 = (_local3 as GameQuest);
                    this.displayQuestPopup(_local4.name, _local4, false);
                };
            };
        }
        public function expireSpecificQuest(_arg1:String):void{
            this.m_expiredQuests.push(_arg1);
            Global.hud.removeQuestSprite(_arg1);
        }
        protected function displayQuestPopup(_arg1:String, _arg2:GameQuest, _arg3:Boolean):void{
            var _local4:Object = _arg2.getPopupData();
            if (_local4 == null){
                return;
            };
            var _local5:Class = QuestPopup;
            var _local6:Class = (_arg3) ? _arg2.getCustomQuestCompleteDialog() : _arg2.getCustomQuestDialog();
            if (_local6){
                _local5 = _local6;
            };
            if (_arg2.popNews){
                if (!_arg2.hasDoneTask("popNews", _local4.tasks, _local4.progress)){
                    this.showNews(_arg1, _arg2.newsImageUrl);
                };
            } else {
                if (((_arg2.hasIntro) && (!(_arg2.hasDoneTask("seenQuest", _local4.tasks, _local4.progress))))){
                    this.showQuestIntro(_local4, _arg1);
                    _arg2.resetQuestIcon();
                    _local4 = _arg2.getPopupData();
                };
                if (_arg2.shouldShowPopup(_arg3)){
                    UI.displayPopup(new _local5(_local4, _arg3), true, _arg1, true);
                    if (((!(_arg2.hasIntro)) && (!(_arg2.hasDoneTask("seenQuest", _local4.tasks, _local4.progress))))){
                        GameTransactionManager.addTransaction(new TSeenQuest(_arg1), true);
                    };
                };
            };
        }
        protected function displayEpiloguePopups(_arg1:GameQuest, _arg2:Array=null):void{
            var _local6:int;
            var _local7:Object;
            var _local8:Number;
            var _local9:Class;
            var _local3:Array = _arg1.getEpilogueDialogs();
            var _local4:Array = _arg1.getEpiloguePopupData();
            if (((!((_arg2 == null))) && ((_arg2.length > 0)))){
                _local6 = 0;
                while (_local6 < _local4.length) {
                    if (_arg2.length <= _local6){
                        _local4[_local6].url = _arg2[(_arg2.length - 1)];
                    } else {
                        _local4[_local6].url = _arg2[_local6];
                    };
                    _local6++;
                };
            };
            if (((!(_local3)) || ((_local3.length <= 0)))){
                return;
            };
            if (((!(_local4)) || (!((_local4.length == _local3.length))))){
                return;
            };
            var _local5:int;
            while (_local5 < _local3.length) {
                _local7 = _local4[_local5];
                _local8 = 0;
                if (_local7.delay != null){
                    _local8 = _local7.delay;
                };
                _local9 = _local3[_local5];
                if (_local9){
                    this.displayEpiloguePopup(_local9, _local7, _local8);
                };
                _local5++;
            };
        }
        private function displayEpiloguePopup(_arg1:Class, _arg2:Object, _arg3:Number):void{
            var epilogueClass:* = _arg1;
            var popupObject:* = _arg2;
            var delay:* = _arg3;
            TimerUtil.callLater(function ():void{
                UI.displayPopup(new epilogueClass(popupObject));
            }, (delay * 1000));
        }
        protected function showQuestIntro(_arg1:Object, _arg2:String):void{
            var _local3:String = ZLoc.t("Quest", _arg1.intro.@text, this.getLocObjects());
            var _local4:String = ZLoc.t("Quest", (_arg1.intro.@text + "_title"), this.getLocObjects());
            var _local5:QuestIntroDialog = new QuestIntroDialog(_local3, _local4, 0, _arg1.introCloseCallback, true, _arg1.npcIntroUrl);
            UI.displayPopup(_local5);
            GameTransactionManager.addTransaction(new TSeenQuest(_arg2), true);
        }
        protected function showNews(_arg1:String, _arg2:String):void{
            var _local3:Object = this.getLocObjects();
            var _local4:String = ZLoc.t("Quest", (_arg1 + "_newsContent"), _local3);
            var _local5:String = ZLoc.t("Quest", (_arg1 + "_newsSubHead"), _local3);
            UI.displayNewsFlash(_local4, 0, _arg2, _local5);
            GameTransactionManager.addTransaction(new TPopNews(_arg1), true);
        }
        public function getLocObjects():Object{
            var _local1:String = ((Global.player.snUser.gender)=="M") ? ZLoc.instance.MASC : ZLoc.instance.FEM;
            return ({
                user:ZLoc.tn(Global.player.firstName, _local1),
                cityname:Global.player.cityName
            });
        }
        public function startUXLocking():void{
            Global.player.setSeenFlag(QUEST_UX_LOCKING_STARTED);
            var _local1:Array = Global.world.getObjectsByClass(Residence);
            var _local2:Array = Global.world.getObjectsByClass(Business);
            var _local3:Array = _local1.concat(_local2);
            var _local4:MapResource;
            this.refreshBuildings(_local3);
        }
        public function unlockUX(_arg1:String):void{
            if (!ArrayUtil.arrayContainsValue(ALL_QUEST_UX_FLAGS, _arg1)){
                throw (new Error(("Invalid quest ux flag: " + _arg1)));
            };
            Global.player.setSeenFlag(_arg1);
            var _local2:Array = Global.world.getObjectsByClass(Residence);
            var _local3:Array = Global.world.getObjectsByClass(Business);
            var _local4:Array = _local2.concat(_local3);
            this.refreshBuildings(_local4);
        }
        protected function refreshBuildings(_arg1:Array):void{
            var _local2:MapResource;
            while (_arg1.length > 0) {
                _local2 = _arg1.pop();
                _local2.setState(_local2.getState());
            };
        }
        public function isFlagReached(_arg1:String):Boolean{
            return (((Global.player.getSeenFlag(_arg1)) || (Global.player.getSeenSessionFlag(_arg1))));
        }
        public function isUXUnlocked(_arg1:String):Boolean{
            if (!ArrayUtil.arrayContainsValue(ALL_QUEST_UX_FLAGS, _arg1)){
                throw (new Error(("Invalid quest ux flag: " + _arg1)));
            };
            return (((!(this.isFlagReached(QUEST_UX_LOCKING_STARTED))) || (this.isFlagReached(_arg1))));
        }
        public function manualQuestStarted_FinalSetup(_arg1:String):void{
            StatsManager.milestone(this.getMilestoneName(_arg1, "begin"));
            switch (_arg1){
                case "holiday_tree1":
                    if (!HolidayTree.doesPlayerHaveTree()){
                        HolidayTree.instance.trackAction("tree_issued");
                        Global.player.inventory.addItems(Item.HOLIDAY2010_TREE_ITEM, 1, true);
                    };
                    break;
                case "neighbor_gate_quest_2":
                    break;
                case "qm_storage_warehouse":
                    Global.player.inventory.addSingletonItem("warehouse", true);
                    break;
                case "qm_visitor_center":
                    Global.player.inventory.addSingletonItem("mun_visitorcenter", true);
                    break;
                case "qm_factory_1":
                    Global.player.inventory.addSingletonItem("factory_premiumgoods", true);
                    break;
                case "qm_clerk_office":
                    Global.player.inventory.addSingletonItem("mun_clerkoffice", true);
                    break;
                case "qm_hardware1":
                    Global.player.inventory.addSingletonItem("bus_hardwarestore", true);
                    break;
                case "qm_cruiseship_dock":
                    Global.player.inventory.addSingletonItem("dock_house", true);
                    break;
                case "qm_zoo_1":
                    Global.player.inventory.addSingletonItem("enclosure_jungle", true);
                    break;
                case "qm_peddler_center":
                    Global.player.inventory.addSingletonItem("mun_peddlercenter", true);
                    break;
                case "qm_bridge_1":
                    Global.player.inventory.addSingletonItem("bridge_standard", true);
                    break;
                case "qm_promo_kungfupanda":
                    Global.player.inventory.addSingletonItem("bus_drivein", true);
                    break;
                case "qf_mall_construction":
                    Global.player.inventory.addSingletonItem("mall", true);
                    break;
            };
        }
        public function isQuestActive(_arg1:String):Boolean{
            var _local3:Object;
            var _local2:Vector.<Quest> = this.getIncompleteActiveQuests();
            for each (_local3 in _local2) {
                if ((((_local3 is GameQuest)) && ((_local3.name == _arg1)))){
                    return (true);
                };
            };
            return (false);
        }

    }
}//package Modules.quest.Managers 
